1358D - The Best Vacation - CodeForces Solution


binary search brute force greedy implementation two pointers *1900

Please click on ads to support us..

Python Code:

from sys import stdin
input = stdin.readline





def answer():

    prefix = [0]
    psum = [0]

    for i in range(n):

        prefix.append(prefix[-1] + a[i])
        psum.append(psum[-1] + (a[i] * (a[i] + 1))//2)


    ans = 0
    for i in range(n):

        l , h = i + 1 , 2 * n
        val , sub , upper = 0 , 0 , i
        while(l <= h):

            mid = (l + h) // 2

            if(mid <= n):value = prefix[mid] - prefix[i]
            else:value = prefix[n] - prefix[i] + prefix[mid - n]
            
            if(value <= d):
                
                if(mid <= n):val = psum[mid] - psum[i]
                else:val = psum[n] - psum[i] + psum[mid - n]

                sub = value
                upper = mid
                
                l = mid + 1
            else:
                h = mid - 1


        req = d - sub
        if(req == 0):
            ans = max(ans , val)
            continue

        upper %= n

        buffer = a[upper] - req
        val += (a[upper] * (a[upper] + 1))//2
        val -= (buffer * (buffer + 1))//2

        ans = max(ans , val)
        
    return ans

     
for T in range(1):

    n , d = map(int,input().split())

    a = list(map(int,input().split()))

    print(answer())

    

C++ Code:

#include <bits/stdc++.h>
 
using namespace std;
 
long long triangleNumber(long long x)
{
    return x * (x + 1) / 2;
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, l;
    long long x;
    long long maxHugs = 0LL;
    long long currHugs = 0LL;
    cin >> n >> x;
    int d[2 * n];
    long long pd[2 * n + 1];
    pd[0] = 0LL;
    for (int i = 0; i < n; ++i)
    {
        cin >> d[i];
        d[n + i] = d[i];
    }
    for (int i = 0; i < (n << 1); ++i)
    {
        pd[i + 1] = pd[i] + d[i];
    }
    l = 0;
    for (int r = 1; r <= 2 * n; ++r)
    {
        currHugs += triangleNumber(d[r - 1]);
        if (pd[r] < x)
        {
            continue;
        }
        while (l < r && pd[r] - pd[l] > x)
        {
            currHugs -= triangleNumber(d[l]);
            ++l;
        }
        if (pd[r] - pd[l] == x)
        {
            maxHugs = max(maxHugs, currHugs);
        }
        else
        {
            maxHugs = max(maxHugs, currHugs + triangleNumber(d[l - 1])
            - triangleNumber(d[l - 1] - (x - pd[r] + pd[l])));
        }
    }
    cout << maxHugs << endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II